import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**战斗角色初始值属性枚举*/
export const enum E_BattleActorInitValue {
	/**血量*/
	HP = 0,
	/**蓝量*/
	MANA = 1,
	/**枚举数量*/
	EnumCount = 2
}


/**战斗角色初始值*/
export class BattleActorInitValue {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_BATTLE_ACTOR_INIT_VALUE_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_BATTLE_ACTOR_INIT_VALUE_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: BattleActorInitValue[] = [];

	/**创建入口*/
	public static create(json?: string): BattleActorInitValue {
		const c = this.POOL.pop() || new BattleActorInitValue();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**血量 (long)*/
	protected _hp: number;
	/**蓝量 (long)*/
	protected _mana: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (BattleActorInitValue.POOL.indexOf(this) === -1) {
			BattleActorInitValue.POOL.push(this);
		}
	}

	/**血量*/
	public get hp() {
		return this._hp;
	}


	/**血量*/
	public set hp(v: number) {
		this._hp = v;
	}


	/**蓝量*/
	public get mana() {
		return this._mana;
	}


	/**蓝量*/
	public set mana(v: number) {
		this._mana = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_BattleActorInitValue): any {
		switch (k) {
			case E_BattleActorInitValue.HP: return this._hp;
			case E_BattleActorInitValue.MANA: return this._mana;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_BattleActorInitValue, v: any): void {
		switch (k) {
			case E_BattleActorInitValue.HP: this._hp = v; break;
			case E_BattleActorInitValue.MANA: this._mana = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//血量
		this._hp = 0;
		//蓝量
		this._mana = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._hp) {
			json.hp = this._hp;
		}
		if (this._mana) {
			json.mana = this._mana;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.hp) {
			this._hp = json.hp;
		}
		if (json.mana) {
			this._mana = json.mana;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeLongNumber(this._hp);
		byte.writeLongNumber(this._mana);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._hp = byte.readLongNumber();
		this._mana = byte.readLongNumber();
		return this;
	}


	/**克隆*/
	public clone(): BattleActorInitValue {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = BattleActorInitValue.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = BattleActorInitValue.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//血量
			if (idx === E_BattleActorInitValue.HP) {
				const oldv = this._hp;
				this._hp = us[i + 1];
				IAsync.dispatch(eventID, idx, this._hp, oldv);
				i += 2;
				continue;
			}
			//蓝量
			if (idx === E_BattleActorInitValue.MANA) {
				const oldv = this._mana;
				this._mana = us[i + 1];
				IAsync.dispatch(eventID, idx, this._mana, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in BattleActorInitValue with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: BattleActorInitValue): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._hp !== other._hp) {
			updates.push(E_BattleActorInitValue.HP, this._hp);
		}
		if (this._mana !== other._mana) {
			updates.push(E_BattleActorInitValue.MANA, this._mana);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(BattleActorInitValue.TOTAL_EVENT_ID, this.getV(0));
	}

}
